<?php
//文件名：lvmao_keyword.php
//用途：记录用户访问的每一篇文章，分析关键词并记录用户浏览兴趣，用于智能推荐


require_once 'lvmao_func.php';



//更新关键词浏览数
function UpdateTagCount($keys)
{
	global $db;
	
	if($keys!="")
	{	
		$keyarr=split(" ",$keys);
		foreach ( $keyarr as $key )
		{
			$sql="update ".DB_PRE."keyword set viewcount=viewcount+1 where tag='".$key."';";
			$db->query($sql);
			//echo $sql."||";
		}
	}
	
}



//记录用户浏览关键词
function AddUserTag($uid,$keywords)
{
	//用户未登录
	if($uid==0 || $uid=="")
	{
		//echo "未登录用户";
		return;
	}
	
	global $db;
	
	//查询是否有此用户信息
	$sql="select count(*) as countx from user_tag where uid=".$uid;
	//echo $sql;
	$result=$db->query($sql);
	$result=$db->fetch_array($result);
	//echo $result[countx];
	
	
	//如果没有则添加一个
	if(intval($result[countx])<1)
	{
		$sql="insert into user_tag(uid,tagcache,taglog,cachetime) values(".$uid.",'','','".time()."');";
		$db->query($sql);
		//echo "<br />新加一个用户行";
	}
	
	
	//更新用户关键词记录
	$sql="update user_tag set taglog=concat('".$keywords." ',taglog) where uid=".$uid;
	$db->query($sql);
	//echo "<br />".$sql."||logged:".$keywords;
	
	
	//更新关键词浏览量
	UpdateTagCount($keywords);
	//调用函数重新计算排序
	UpdateUserTag($uid);

}




//重新计算用户关键词排序
function UpdateUserTag($uid)
{
	global $db;
	
	
	//查询用户关键词信息
	$sql="select * from user_tag where uid=".$uid;
	$result=$db->query($sql);
	$result=$db->fetch_array($result);
	
	
	//获得最后更新时间
	$c1=intval(time());
	$c2=intval($result[cachetime]);
	$c=$c1-$c2;
	
	//判断更新时间间隔
	if($c<3)
	{
		//echo "<br/>no longer than 3";
		return;
	}
	
	
	//=========开始更新排序========
	
	
	$keyarr=split(" ",$result[taglog]);
	$keystr="##";
	
	foreach ( $keyarr as $key )
	{
		
		$pat = "/##(".$key."),(\d+)##/";
		$recount=preg_match_all($pat,$keystr,$re,1);
		
		//echo "<br/>".$recount.$pat.$key;
		
		//echo "<br/>开始处理：".$keystr;
		if($recount==1)
		{
			$keyx=$re[1][0];
			$keyy=$re[2][0];
			$keyy=intval($keyy)+1;
			
			$keytemp="##".$keyx.",".$keyy."##";
			
			$keystr=preg_replace($pat,$keytemp,$keystr);
			
			//echo "替换";
		}
		else
		{
			$keystr=$keystr.$key.",1##";
			//echo "增加";
		}
		//echo $keystr;
		
	}
	
	$keystr=preg_replace("/^#+(.*?)#+$/","$1",$keystr);
	//echo "<br>正则".$keystr;
	
	//初步拆分得到数组
	$keyresult=split("##",$keystr);
	$n=0;
	
	//定义结果数组
	$keyre=array();
	
	foreach ( $keyresult as $kr )
	{
		$spl=split(",",$kr);
		
		//写入结果数组
		$keyre[$spl[0]]=$spl[1];
		
		//echo "<br/>".$keyresult[n][1];
	}
	
//	echo "<br/>===".$keyre["1239814661"];
//	
//	foreach ( $keyre as $keys )
//	{
//		echo "<br/>".$keys["times"];	
//	}
	
	//对结果数组进行排序
	arsort($keyre,SORT_NUMERIC);
	//array_multisort($keyre,SORT_DESC,SORT_NUMERIC);
	
	//echo "<br/>===".$keyre["1239814661"];
	$n=0;
	$tagcache=",";

	foreach ( $keyre as $keys => $val)
	{
		if($n>5){break;}
		$n=$n+1;
		
		//echo $tagcache;
//		echo "<br/>".$keys.$val;
		$tagcache=$tagcache.$keys.",";
	}
	
	//echo $tagcache;
	$tagcache=preg_replace("/^,(.*),,$/","$1",$tagcache);
	if($tagcache==","){$tagcache="";}
	
	//echo "<br/>".$tagcache;
	
	$sql="update user_tag set tagcache='".$tagcache."',cachetime=".time()." where uid=".$uid;
	$db->query($sql);
	
	//echo "<br />order update!";
}

function mysort($a,$b)
{
	if ($a == $b) return 0;
  	return ($a < $b) ? -1 : 1;
}
?>